home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTDELCUR.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  121 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btdelcur.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STDLIB
  14. #include <stdlib.h>
  15. #endif
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19.  
  20. /* library headers */
  21. #include <blkio.h>
  22. #include <bool.h>
  23.  
  24. /* local headers */
  25. #include "btree_.h"
  26.  
  27. /*man---------------------------------------------------------------------------
  28. NAME
  29.      btdelcur - delete current btree key
  30.  
  31. SYNOPSIS
  32.      #include <btree.h>
  33.  
  34.      int btdelcur(btp)
  35.      btree_t *btp;
  36.  
  37. DESCRIPTION
  38.      The btdelcur function deletes the current key from btree btp.
  39.      The cursor is positioned to null.
  40.  
  41.      btdelcur will fail if one or more of the following is true:
  42.  
  43.      [EINVAL]       btp is not a valid btree pointer.
  44.      [BTELOCK]      btree btp is not write locked.
  45.      [BTENKEY]      The cursor is null.
  46.      [BTENOPEN]     btree btp is not open.
  47.  
  48. SEE ALSO
  49.      btdelete, btinsert, btsearch.
  50.  
  51. DIAGNOSTICS
  52.      Upon successful completion, a value of 0 is returned.  Otherwise,
  53.      a value of -1 is returned, and errno set to indicate the error.
  54.  
  55. ------------------------------------------------------------------------------*/
  56. #ifdef AC_PROTO
  57. int btdelcur(btree_t *btp)
  58. #else
  59. int btdelcur(btp)
  60. btree_t *btp;
  61. #endif
  62. {
  63.     int        found    = 0;        /* key found flag */
  64.     void *        key    = NULL;        /* key */
  65.  
  66.     /* validate arguments */
  67.     if (!bt_valid(btp)) {
  68.         errno = EINVAL;
  69.         return -1;
  70.     }
  71.  
  72.     /* check if not open */
  73.     if (!(btp->flags & BTOPEN)) {
  74.         errno = BTENOPEN;
  75.         return -1;
  76.     }
  77.  
  78.     /* check lock */
  79.     if (!(btp->flags & BTWRLCK)) {
  80.         errno = BTELOCK;
  81.         return -1;
  82.     }
  83.  
  84.     /* check if cursor is null */
  85.     if (btcursor(btp) == NULL) {
  86.         errno = BTENKEY;
  87.         return -1;
  88.     }
  89.  
  90.     /* generate search path if necessary */
  91.     if (btp->cbtnp->n < bt_ndmin(btp) + 1) {
  92.         key = calloc((size_t)1, btp->bthdr.keysize);
  93.         if (key == NULL) {
  94.             BTEPRINT;
  95.             return -1;
  96.         }
  97.         memcpy(key, bt_kykeyp(btp, btp->cbtnp, btp->cbtpos.key), btp->bthdr.keysize);
  98.         found = btsearch(btp, key);
  99.         if (found == -1) {
  100.             BTEPRINT;
  101.             free(key);
  102.             return -1;
  103.         }
  104.         free(key);
  105.         key = NULL;
  106.         if (found == 0) {
  107.             BTEPRINT;
  108.             errno = BTEPANIC;
  109.             return -1;
  110.         }
  111.     }
  112.  
  113.     /* delete the current key */
  114.     if (bt_delete(btp) == -1) {
  115.         BTEPRINT;
  116.         return -1;
  117.     }
  118.  
  119.     return 0;
  120. }
  121.